package com.gsww.vue.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gsww.vue.entity.sys.SysDepartment;
import com.gsww.vue.entity.sys.SysMenu;
import com.gsww.vue.entity.sys.SysUserSession;
import com.gsww.vue.service.sys.SysDepartmentService;
import com.gsww.vue.utils.StringHelper;
import com.gsww.vue.utils.TimeHelper;

@RestController
@RequestMapping("/sys")
public class SysDepartmentController {
	@Autowired
	private SysDepartmentService sysDepartmentService;
	/**
	 * 保存用户信息
	 */
	@RequestMapping(value = "/departmentSave", method = RequestMethod.POST)
	//@RequestParam String  userAcctId,@RequestParam String  deptId, @RequestParam SysAccount sysAccount
	//@RequestBody Map<String,Object> params
	public Map<String,Object> accountSave(@RequestBody SysDepartment  sysDepartment ,HttpServletRequest request,HttpServletResponse response)  throws Exception {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if (!StringHelper.isNotBlack(sysDepartment.getDeptState())) {
				sysDepartment.setDeptState("1");
				sysDepartment.setCreateTime(TimeHelper.getCurrentTime());
			}
			SysDepartment dept = sysDepartmentService.save(sysDepartment);
			resMap.put("deptCode",dept.getDeptCode());
			resMap.put("success",true);
		} catch (Exception e) {
			resMap.put("success",false);
			e.printStackTrace();
		} 
		return resMap;
	}
	/**
	 * 返回完整的组织机构树的数据
	 * 按照setid（帐套的物理id过滤）
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/departmenttree", method = RequestMethod.GET)
	public Map<String,Object> departmenttree(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
		String id = request.getParameter("id");
		String deptCode = request.getParameter("deptCode");
		System.out.println("id="+id);
		System.out.println("deptCode="+deptCode);
		System.out.println();
		String update = request.getParameter("update");//是否是新增或修改  节点
		try {
			//SysUserSession sysUserSession = (SysUserSession) request.getSession().getAttribute("sysUserSession");
			List<SysDepartment> list = new ArrayList<SysDepartment>(50);
			
			if(StringUtils.isNotBlank(update)) { 
				List<String> deptList = findParentDept(new ArrayList<String>(),deptCode);
//				for(int i=0; i<deptList.size(); i++) {
//					if(i==0) {
//						list = sysDepartmentService.findByDeptCode(deptCode);
//						treeList = createTreeData(list);
//					}
//				}
				System.out.println(deptList);
				treeList = createTreeDataTwo(deptList,0);
			} else {
				if(StringUtils.isBlank(id) && StringUtils.isNotBlank(deptCode)) {//首次加载本级
					//id = sysUserSession.getDeptCode();
					list = sysDepartmentService.findByDeptCode(deptCode);
					//查下级
//					List<SysDepartment> children = sysDepartmentService.findByParentDeptCode(deptCode);
				} else {
					list = sysDepartmentService.findByParentDeptCode(id);//获取子级
					
				}
				treeList = createTreeData(list);
			}
			//System.out.println(id);
			//SysUserSession sysUserSession = (SysUserSession) ((HttpServletRequest) request).getSession().getAttribute("sysUserSession");
			resMap.put("treeList", treeList);
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	private List<Map<String, Object>> createTreeDataTwo(List<String> codeList,int index) throws Exception{
		List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
		List<SysDepartment> list = new ArrayList<SysDepartment>(50);
		//for(int i=0; i<codeList.size(); i++) {
			
		//List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
		if(index==0) {
			Map<String, Object> topMap = new HashMap<String, Object>();
			list = sysDepartmentService.findByDeptCode(codeList.get(index));
			if (list != null && !list.isEmpty()) {
				SysDepartment dept =  list.get(0);
				topMap.put("id", dept.getDeptId());
				topMap.put("pId", dept.getParentDeptCode());
				topMap.put("name", dept.getDeptName());
				topMap.put("title", dept.getDeptName());
				topMap.put("deptCode", dept.getDeptCode());
				topMap.put("xzqh", dept.getXzqh());
				topMap.put("loading", false);
				topMap.put("hasChildren", true);
				topMap.put("children", new ArrayList());
				index++;
			}
			treeList.add(topMap);
			//createTreeDataTwo(codeList,++index);
		} else {  //index =1 获取软件事业部 下的
			List<SysDepartment> childrenList = sysDepartmentService.findByParentDeptCode(codeList.get(index-1));//获取子级
			List<Map<String, Object>> cList = new ArrayList<Map<String, Object>>();
//			for (SysDepartment sysDepartment : childrenList) {
//				Map<String, Object> map = new HashMap<String, Object>();
//				map.put("id", sysDepartment.getDeptId());
//				map.put("pId", sysDepartment.getParentDeptCode());
//				map.put("name", sysDepartment.getDeptName());
//				map.put("title", sysDepartment.getDeptName());
//				map.put("deptCode", sysDepartment.getDeptCode());
//				map.put("xzqh", sysDepartment.getXzqh());
//				List<SysDepartment> deptList = sysDepartmentService.findByDeptCode(sysDepartment.getParentDeptCode());
//				if(!deptList.isEmpty()) {
//					map.put("parentDeptName", deptList.get(0).getDeptName());
//				}
//				map.put("regiontype", "1");
//				map.put("seq", String.valueOf(sysDepartment.getDeptSeq()));
//				if(codeList.get(index).equals(sysDepartment.getDeptCode())) {
//					map.put("expand", true);
//				}
//					List<SysDepartment> subList = sysDepartmentService.findByParentDeptCode(sysDepartment.getDeptCode());
//					if(!subList.isEmpty()) {
//						map.put("children", new ArrayList());
//						map.put("loading", false);
//						map.put("hasChildren", true);
//					} else {
//						map.put("hasChildren", false);
//					}
//					cList.add(map);
//					topMap.put("children", cList);
//			}
			
		} 

		return treeList;
	}
	private List<Map<String, Object>> createTreeData(List<SysDepartment> list) throws Exception{
		List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
		if (list != null && !list.isEmpty()) {
			for (SysDepartment sysDepartment : list) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", sysDepartment.getDeptId());
				map.put("pId", sysDepartment.getParentDeptCode());
				map.put("name", sysDepartment.getDeptName());
				map.put("title", sysDepartment.getDeptName());
				map.put("deptCode", sysDepartment.getDeptCode());
				map.put("xzqh", sysDepartment.getXzqh());
				List<SysDepartment> deptList = sysDepartmentService.findByDeptCode(sysDepartment.getParentDeptCode());
				if(!deptList.isEmpty()) {
					map.put("parentDeptName", deptList.get(0).getDeptName());
				}
				map.put("regiontype", "1");
				map.put("seq", String.valueOf(sysDepartment.getDeptSeq()));
				List<SysDepartment> subList = sysDepartmentService.findByParentDeptCode(sysDepartment.getDeptCode());
				if(!subList.isEmpty()) {
					//map.put("children", new ArrayList());
					map.put("children", createTreeData(subList));
					map.put("loading", false);
					map.put("hasChildren", true);
				} else {
					map.put("hasChildren", false);
				}
				treeList.add(map);
			}
		}
		return treeList;
	}
	private List<String> findParentDept(List<String> list,String deptCode) {
		List<String> newList = new ArrayList<String>();
		try {
			SysDepartment dept = sysDepartmentService.findByCode(deptCode);
			
			if(null != dept) {
				list.add(dept.getDeptCode());
				
				findParentDept(list,dept.getParentDeptCode());
			}
			
			for(int i=list.size()-1; i>=0; i--) {
				newList.add(list.get(i));
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return newList;
	}
	/**计算新部门的编码和排序
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/getDeptCode", method = RequestMethod.GET)
	public Map<String,Object> menuDelete(@RequestParam String  parentDeptCode, HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if(StringHelper.isNotBlack(parentDeptCode)){
				resMap.put("data", sysDepartmentService.getDeptCode(parentDeptCode));
			}
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	@RequestMapping(value = "/deptDelete", method = RequestMethod.GET)
	public Map<String,Object> deptDelete(@RequestParam String  id, HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if(StringHelper.isNotBlack(id)){
				SysDepartment sysDept = sysDepartmentService.findByKey(id);
				sysDept.setDeptState("0");
				sysDepartmentService.save(sysDept);
			}
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
}
